// pages/bill/bill.js
import Butils from '../../utils/detailsUtil'
import ProjectUtil from '../../utils/projectUtil'
import utils from '../../utils/util'
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    projectInfo: null,
    list: [],
    amount: 0,
    inAmount: 0,
    outAmount: 0,
    ShowProjectEnd: false,
    modalName: null,
  },
  /**
   * 获取项目列表
   * @param {*} projectId 项目id
   */
  getList: function (projectId) {
    let that = this;
    let innum = 0;
    let outnum = 0;
    Butils.getDetailsByProjectId(app.globalData.wxUserInfo.id, projectId, (res) => {
      res.data.objects.forEach(e => {
        if (e.bill_type.is_income) {
          innum += e.amount
        } else {
          outnum += e.amount
        }
      });
      that.setData({
        list: res.data.objects,
        amount: parseFloat(innum - outnum).toFixed(2),
        inAmount: parseFloat(innum).toFixed(2),
        outAmount: parseFloat(outnum).toFixed(2)
      })
    })
  },
  /**
   * 完成
   */
  ShowEnd: function (e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  /**
   * 显示开启弹框
   */
  ShowStart: function (e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  /**
   * 隐藏模态框 
   * @param {*} params 
   */
  hideModal: function (params) {
    this.setData({
      modalName: null
    })
  },
  /**
   * 完成
   */
  EndProject: function (params) {
    ProjectUtil.endProject(this.data.projectInfo.id, (res) => {
      this.setData({
        modalName: null,
        ShowProjectEnd: false
      })
    })
  },
  /**
   * 重新开启项目
   * @param {*} params 
   */
  StartProject: function (params) {
    ProjectUtil.startProject(this.data.projectInfo.id, (res) => {
      this.setData({
        modalName: null,
        ShowProjectEnd: true
      })
    })
  },
  /**
   * 返回
   */
  Back: function (params) {
    wx.navigateBack({ changed: true });
  },
  /**
   * 查看账单明细
   * @param {*} params 
   */
  BillInfo: function (e) {
    app.globalData.billInfo = e.target.dataset.target
    wx.navigateTo({
      url: '../bill/billInfo/billInfo'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getList(app.globalData.projectId);
    ProjectUtil.getProject(app.globalData.projectId, (res) => {
      this.setData({
        projectInfo: res.data
      })
      if (!res.data.is_end) {
        this.setData({
          ShowProjectEnd: true
        })
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})